<?php

namespace app\modules\admin\controllers;

use Yii;
use app\models\History;
use app\models\search\HistorySearch;
use app\modules\admin\controllers\AdminBaseController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;

/**
 * HistoryController implements the CRUD actions for History model.
 */
class HistoryController extends AdminBaseController
{
    /**
     * @inheritdoc
     */
//    public function behaviors()
//    {
//        return [
//            'verbs' => [
//                'class' => VerbFilter::className(),
//                'actions' => [
//                    'delete' => ['POST'],
//                ],
//            ],
//            'access' => [
//                'class' => AccessControl::className(),
//                'rules' => [
//                    [
//                        'actions' => ['index', 'view', 'create', 'update', 'delete'],
//                        'allow' => true,
//                        'roles' => ['@'],
//                    ],
//                ],
//            ],
//        ];
//    }

    /**
     * Lists all History models.
     * @return mixed
     */
    public function actionIndex()
    {
        $this->setForward();
        $searchModel = new HistorySearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single History model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new History model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new History();
        $model->loadDefaultValues();
        if ($model->load(Yii::$app->request->post())) {
            $model->time = strtotime($model->time);
            $model->pics = is_array($model->pics) ? implode(',', $model->pics) : '';
            if ($model->save()) {
                $this->success('操作成功', $this->getForward(), true);
            } else {
                $msg = '';
                foreach ($model->getFirstErrors() as $v) {
                $msg .= $v;
                }
                $this->error($msg,'#',true);
            }
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing History model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        if ($model->load(Yii::$app->request->post())) {
            $model->time = strtotime($model->time);
            $model->pics = is_array($model->pics) ? implode(',', $model->pics) : '';
            if ($model->save()) {
                $this->success('操作成功', $this->getForward(), true);
            } else {
                $this->error('操作错误','#', true);
            }
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing History model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionDelete()
    {
        $model = $this->findModel(0);
        if($this->delRow($model, 'id')) {
            $this->success('删除成功', $this->getForward(), true);
        } else {
            $this->error('删除失败！','#', true);
        }
    }

    /**
     * Finds the History model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return History the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if($id == 0) {
            return new History();
        }
        if (($model = History::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
